<?php
declare (strict_types = 1);
/**
 * 管理员
 */
namespace app\common\admin;

use app\common\facade\model\ConfigModel;
use app\common\facade\model\DeviceModel;

class System extends BaseAdmin
{
    // 读取基本信息
    public function baseinfo() {
        $info = ConfigModel::allinfo();
        $baseinfo = [];
        if(!empty($info)) {
            foreach($info as $k => $v) {
                $baseinfo[$v['name']] = ($v['value'] == null || $v['value']== '') ? $v['default'] : $v['value'];
            }
        }
        return $baseinfo;
    }

    public function baseseting($data) {
        if(!empty($data)) {
            foreach($data as $k => $v) {
                $where = [['name', '=', $k]];

                $param = [];
                $param['name'] = $k;
                $param['value'] = $v;
                ConfigModel::addEdit($param, $where);
            }
        }
        return true;
    }

    /* 设备列表 */
    public function deviceList($param = []) {
        $where = [];

        if (isset($param['name']) && !empty($param['name'])) $where[] = ['dev_name', 'like', "%{$param['name']}%"];

        $deviceList = DeviceModel::deviceList($where, $param['offset'], $param['length']);

        return $deviceList;
    }

    /* 设备编辑 */
    public function deviceEdit($param = []) {
        $where = [];
        if(isset($param['id'])):
            $where[] = ['id', '=', $param['id']];
            unset($param['id']);
        endif;
  
        return DeviceModel::addEditSave($param, $where);
    }

    /* 设备删除 */
    public function deviceDel($ids) {
        // 当ids 不为空时
        if(!empty($ids)) {
            $res = DeviceModel::deleteData([['id', 'in', $ids]]);
        } else {
            return [
                'code' => 0,
                'msg'  => '参数未定义'
            ];
        }

        return [
            'code' => $res ? 1 : 0,
            'msg'  => $res ? '删除成功' : '删除失败'
        ];
    }
}